﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class class_ISYC_202_01T
    {

        public static void ISYC_202_01T(Dictionary<string, Object> data)  //切槽切断刀片，单切削刃切槽，螺钉安装
        {
            DxfDocument dxf = new DxfDocument();
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);

            //参数
            double sbr = (Convert.ToDouble(data["insl"]) - Convert.ToDouble(data["pdy"])) * 2;
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            //double scale = 1; //放大系数
            if (scale == 0 && sbr < 29.7)
            {
                scale = 2.0;
            }
            else if (scale == 0 && sbr < 39.6)
            {
                scale = 1.5;
            }
            else if (scale == 0 && sbr < 59.4)
            {
                scale = 1.0;
            }
            else if (scale == 0)
            {
                scale = 0.5;
            }

            double pi = Math.PI;

            double isframe = Convert.ToDouble(data["isframe"]);
            double insl = Convert.ToDouble(data["insl"]) * scale;
            double s = Convert.ToDouble(data["s"]) * scale;
            double gan = Convert.ToDouble(data["gan"]) * pi / 180;
            double d1 = Convert.ToDouble(data["d1"]) * scale;
            double w1 = Convert.ToDouble(data["w1"]) * scale;
            double rer = Convert.ToDouble(data["rer"]) * scale;
            double rel = Convert.ToDouble(data["rel"]) * scale;
            double rar = Convert.ToDouble(data["rar"]) * pi / 180;
            double ral = Convert.ToDouble(data["ral"]) * pi / 180;
            double pdy = Convert.ToDouble(data["pdy"]) * scale;
            double pdx = Convert.ToDouble(data["pdx"]) * scale;
            double pdpt = Convert.ToDouble(data["pdpt"]) * scale;
            double kchtl = Convert.ToDouble(data["kchtl"]) * pi / 180;
            double cw = Convert.ToDouble(data["cw"]) * scale;
            double an = Convert.ToDouble(data["an"]) * pi / 180;

            /*double an = 20 * pi / 180;
            double cw = 2 * scale;
            double kchtl = 45 * pi / 180;
            double pdpt = 3 * scale;
            double pdx = 8 * scale;
            double pdy = 15 * scale;
            double ral = 5 * pi / 180;
            double rar = 5 * pi / 180;
            double rel = 0.5 * scale;
            double rer = 0.5 * scale;
            double w1 = 10 * scale;
            double d1 = 5.16 * scale;
            double gan = 30 * pi / 180;
            double s = 4.76 * scale;
            double insl = 25 * scale;
            double isframe = 0;*/
            double chwtl = pdy * 2 - insl - pdpt;

            double br = insl - pdy;
            double sr = (s * s - 2 * s * br + pdy * pdy) / 2 / (br + pdy * Math.Sin(gan) - (s - br) * Math.Cos(gan));
            double xr = s / 2 / Math.Cos(an * 2) / Math.Sin(an);
            double x1 = 0;
            double x2 = 0;
            string path = Convert.ToString(data["path"]);

            //坐标
            //主视图
            Vector2 v1 = new Vector2(0, -insl + pdy);
            Vector2 v2 = new Vector2(0, -d1 / 2);
            Vector2 v3 = new Vector2(0, d1 / 2);
            Vector2 v4 = new Vector2(0, insl - pdy);
            Vector2 v10 = new Vector2(w1, -pdy);
            Vector2 v11 = new Vector2(v10.X - cw, v10.Y);
            Vector2 v12 = new Vector2(v11.X + pdpt * Math.Tan(ral), v11.Y + pdpt);
            Vector2 v13 = new Vector2(v12.X - chwtl / Math.Tan(kchtl), v1.Y);
            Vector2 v9 = new Vector2(v10.X - (pdpt + chwtl) * Math.Tan(rar), v13.Y);
            Vector2 v8 = new Vector2(v9.X - chwtl / Math.Tan(kchtl), v9.Y + chwtl);
            Vector2 v7 = new Vector2(v8.X, v2.Y);
            Vector2 v6 = new Vector2(v7.X, v3.Y);
            Vector2 v5 = new Vector2(v6.X, v4.Y);
            Vector2 v14 = new Vector2(-2, 0);
            Vector2 v15 = new Vector2(w1 + 2, 0);
            Vector2 v16 = new Vector2(v11.X + rel * Math.Tan(pi / 4 + ral / 2), v11.Y);
            Vector2 v17 = new Vector2(v16.X - rel * Math.Cos(ral), v11.Y + rel + rel * Math.Sin(ral));
            Vector2 v18 = new Vector2(v10.X - rer * Math.Tan(pi / 4 + rar / 2), v10.Y);
            Vector2 v19 = new Vector2(v18.X + rer * Math.Cos(rar), v10.Y + rer + rer * Math.Sin(rar));
            Vector2 v20 = new Vector2(v16.X, v16.Y + rel);
            Vector2 v21 = new Vector2(v18.X, v18.Y + rer);
            Vector2 v22 = new Vector2(v11.X, v11.Y - 5);
            Vector2 v23 = new Vector2(v11.X - 5 * Math.Sin(ral), v11.Y - 5 * Math.Cos(ral));
            Vector2 v24 = new Vector2(v10.X, v10.Y + pdpt);
            Vector2 v25 = new Vector2(v10.X + 59.4 + insl - pdy, v14.Y);
            Vector2 v26 = new Vector2(v10.X + 59.4, v25.Y);
            Vector2 v27 = new Vector2(v26.X, v10.Y + s * Math.Tan(an * 2));
            Vector2 v28 = new Vector2(v26.X + s, v10.Y);
            Vector2 v29 = new Vector2(2 * sr * Math.Cos(pi / 2 - 2 * an - gan) * Math.Cos(pi / 2 - 2 * an) + v28.X, 2 * sr * Math.Cos(pi / 2 - 2 * an - gan) * Math.Sin(pi / 2 - 2 * an) + v28.Y);
            Vector2 v30 = new Vector2(v26.X - 2, v26.Y);
            Vector2 v31 = new Vector2(v25.X + br + 2, v25.Y);
            Vector2 v32 = new Vector2(v25.X, v25.Y + br + 2);
            Vector2 v33 = new Vector2(v25.X, v25.Y - pdy - 2);
            Vector2 v34 = new Vector2(v26.X, v28.Y + s * Math.Tan(an));
            Vector2 v35 = new Vector2(v26.X, v28.Y);
            Vector2 v36 = new Vector2(v28.X, v28.Y - 5);
            Vector2 v37 = new Vector2(v28.X + 5 * Math.Tan(gan), v36.Y);
            Vector2 v42 = new Vector2(v28.X + xr * Math.Sin(an), v28.Y + xr * Math.Cos(an));//大圆心
            Vector2 v43 = new Vector2(v28.X + sr * Math.Cos(gan), v28.Y + sr * Math.Sin(gan));//小圆心
            if (v13.Y - v27.Y > 0)
            {
                x1 = v26.X;
            }
            else if (v13.Y - v27.Y < 0)
            {
                x1 = v42.X - Math.Sqrt(xr * xr - (v42.Y - v13.Y) * (v42.Y - v13.Y));
            }
            else
            {
                x1 = v27.X;
            }
            Vector2 v38 = new Vector2(x1, v13.Y); ;
            Vector2 v39 = new Vector2(v43.X - Math.Sqrt(sr * sr - (v13.Y - v43.Y) * (v13.Y - v43.Y)), v13.Y);
            if (v12.Y - v27.Y > 0)
            {
                x2 = v26.X;
            }
            else if (v12.Y - v27.Y < 0)
            {
                x2 = v42.X - Math.Sqrt(xr * xr - (v42.Y - v12.Y) * (v42.Y - v12.Y));
            }
            else
            {
                x2 = v27.X;
            }
            Vector2 v40 = new Vector2(x2, v12.Y);
            Vector2 v41 = new Vector2(v43.X - Math.Sqrt(sr * sr - (v12.Y - v43.Y) * (v12.Y - v43.Y)), v12.Y);

            //线段
            Vector2[] Line1 = { v1, v2 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { v2, v3 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            Vector2[] Line3 = { v3, v4 };
            fun.drawBLine(dxf, Line3, 0.3, contour);
            Vector2[] Line4 = { v4, v5 };
            fun.drawBLine(dxf, Line4, 0.3, contour);
            Vector2[] Line5 = { v5, v6 };
            fun.drawBLine(dxf, Line5, 0.3, contour);
            Vector2[] Line6 = { v6, v7 };
            fun.drawBLine(dxf, Line6, 0.3, contour);
            Vector2[] Line7 = { v7, v8 };
            fun.drawBLine(dxf, Line7, 0.3, contour);
            Vector2[] Line8 = { v8, v9 };
            fun.drawBLine(dxf, Line8, 0.3, contour);
            Vector2[] Line9 = { v9, v19 };
            fun.drawBLine(dxf, Line9, 0.3, contour);
            Vector2[] Line10 = { v18, v16 };
            fun.drawBLine(dxf, Line10, 0.3, contour);
            Vector2[] Line11 = { v17, v12 };
            fun.drawBLine(dxf, Line11, 0.3, contour);
            Vector2[] Line12 = { v12, v13 };
            fun.drawBLine(dxf, Line12, 0.3, contour);
            Vector2[] Line13 = { v13, v1 };
            fun.drawBLine(dxf, Line13, 0.3, contour);
            Vector2[] Line14 = { v14, v15 };
            fun.drawPlinetype(dxf, Line14, 0.15, center, Linetype.Center);
            Vector2[] Line15 = { v3, v6 };
            fun.drawBLine(dxf, Line15, 0.3, contour);
            Vector2[] Line16 = { v2, v7 };
            fun.drawBLine(dxf, Line16, 0.3, contour);

            LwPolyline Arc1 = new LwPolyline();
            Arc1.Vertexes.Add(new LwPolylineVertex(v16));
            Arc1.Vertexes.Add(new LwPolylineVertex(v17));
            Arc1.Vertexes[0].Bulge = -rel * (1 - Math.Cos(pi / 4 + ral / 2)) / (rel * Math.Sin(pi / 4 + ral / 2));
            fun.PolyFill(dxf, Arc1, 0.3, contour);

            LwPolyline Arc2 = new LwPolyline();
            Arc2.Vertexes.Add(new LwPolylineVertex(v19));
            Arc2.Vertexes.Add(new LwPolylineVertex(v18));
            Arc2.Vertexes[0].Bulge = -rer * (1 - Math.Cos(pi / 4 + rar / 2)) / (rer * Math.Sin(pi / 4 + rar / 2));
            fun.PolyFill(dxf, Arc2, 0.3, contour);

            Arc arc1 = fun.DrawArc(v17, v20, v16);
            Arc arc2 = fun.DrawArc(v18, v21, v19);

            Line Line17 = new Line(v11, v22);
            Line Line18 = new Line(v11, v23);
            Line Line19 = new Line(v10, v24);
            fun.drawcircle(dxf, v25, d1 / 2, 0.3, contour);
            fun.drawcircle(dxf, v25, br, 0.3, contour);
            Vector2[] Line20 = { v26, v27 };
            fun.drawBLine(dxf, Line20, 0.3, contour);

            LwPolyline Arc3 = new LwPolyline();
            Arc3.Vertexes.Add(new LwPolylineVertex(v28));
            Arc3.Vertexes.Add(new LwPolylineVertex(v27));
            Arc3.Vertexes[0].Bulge = -(1 - Math.Cos(an)) / Math.Sin(an);
            fun.PolyFill(dxf, Arc3, 0.3, contour);

            LwPolyline Arc4 = new LwPolyline();
            Arc4.Vertexes.Add(new LwPolylineVertex(v28));
            Arc4.Vertexes.Add(new LwPolylineVertex(v29));
            Arc4.Vertexes[0].Bulge = -sr * (1 - Math.Sin(pi / 2 - 2 * an - gan)) / (sr * Math.Cos(pi / 2 - 2 * an - gan));
            fun.PolyFill(dxf, Arc4, 0.3, contour);

            var circle1 = new Circle(v25, d1 / 2);
            Vector2[] Line21 = { v30, v31 };
            fun.drawPlinetype(dxf, Line21, 0.15, center, Linetype.Center);
            Vector2[] Line22 = { v32, v33 };
            fun.drawPlinetype(dxf, Line22, 0.15, center, Linetype.Center);
            Line Line23 = new Line(v28, v34);
            Line Line24 = new Line(v28, v35);
            Line Line25 = new Line(v28, v36);
            Line Line26 = new Line(v28, v37);
            Vector2[] Line27 = { v38, v39 };
            fun.drawBLine(dxf, Line27, 0.3, contour);
            Vector2[] Line28 = { v40, v41 };
            fun.drawBLine(dxf, Line28, 0.3, contour);

            //图层
            Line17.Layer = dimensioning;
            Line17.Linetype = Linetype.Continuous;
            dxf.AddEntity(Line17);
            Line18.Layer = dimensioning;
            Line18.Linetype = Linetype.Continuous;
            dxf.AddEntity(Line18);
            Line19.Layer = dimensioning;
            Line19.Linetype = Linetype.Continuous;
            dxf.AddEntity(Line19);
            Line23.Layer = dimensioning;
            Line23.Linetype = Linetype.Continuous;
            dxf.AddEntity(Line23);
            Line24.Layer = dimensioning;
            Line24.Linetype = Linetype.Continuous;
            dxf.AddEntity(Line24);
            Line25.Layer = dimensioning;
            Line25.Linetype = Linetype.Continuous;
            dxf.AddEntity(Line25);
            Line26.Layer = dimensioning;
            Line26.Linetype = Linetype.Continuous;
            dxf.AddEntity(Line26);

            //填充
            LwPolyline hatchpoly1 = new LwPolyline();
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v1));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v2));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v7));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v8));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v9));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v10));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v11));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v12));
            hatchpoly1.Vertexes.Add(new LwPolylineVertex(v13));
            hatchpoly1.IsClosed = true;
            String[] htype1 = new String[100];
            Line[] hline1 = new Line[100];
            Arc[] harc1 = new Arc[100];
            int hnumst1 = hatchpoly1.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst1; i++)
            {
                htype1[i] = hatchpoly1.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype1[i] == "Line")
                {
                    hline1[i] = (Line)hatchpoly1.Explode()[i];
                }
                if (htype1[i] == "Arc")
                {
                    harc1[i] = (Arc)hatchpoly1.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype1, hnumst1, 45, 5, hline1, harc1, hatching);

            LwPolyline hatchpoly2 = new LwPolyline();
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v3));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v4));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v5));
            hatchpoly2.Vertexes.Add(new LwPolylineVertex(v6));
            hatchpoly2.IsClosed = true;
            String[] htype2 = new String[100];
            Line[] hline2 = new Line[100];
            Arc[] harc2 = new Arc[100];
            int hnumst2 = hatchpoly2.Explode().Count;  //记录爆炸后，线段有多少
            for (int i = 0; i < hnumst2; i++)
            {
                htype2[i] = hatchpoly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
                if (htype2[i] == "Line")
                {
                    hline2[i] = (Line)hatchpoly2.Explode()[i];
                }
                if (htype2[i] == "Arc")
                {
                    harc2[i] = (Arc)hatchpoly2.Explode()[i];
                }
            }
            fun.hatchsub1(dxf, htype2, hnumst2, 45, 5, hline2, harc2, hatching);

            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = 1.0 / 2;
            double offset = 5;
            double height = 1;
            double arrowsize = 1;
            double exline = 1;

            //线段尺寸标注
            //尺寸1
            /*Angular2LineDimension angdim1 = new Angular2LineDimension(new Line(v1, v43), new Line(v1, v44), offset, dimensionStyle);
            angdim1.UserText = "SIG = " + Math.Round(sig / (pi / 180), 2) + "°";
            dxf.AddEntity(angdim1);*/

            /*LinearDimension dim1 = new LinearDimension(new Line(v11, v12), -offset - pdx - (v12.X - v11.X), 270, dimensionStyle);
            dim1.UserText = "PDPT = " + Math.Round(pdpt / scale, 2);
            dxf.AddEntity(dim1);*/
            fun.dimrot1(dxf, v11, v12, 90, offset + pdx + (v12.X - v11.X), 1, height, arrowsize, exline, "PDPT"/* + Math.Round(pdpt / scale, 2)*/, "", "");

            //尺寸2
            /*LinearDimension dim2 = new LinearDimension(new Line(v11, new Vector2(0, 0)), -offset * 2 - pdx / 2, 270, dimensionStyle);
            dim2.UserText = "PDY = " + Math.Round(pdy / scale, 2);
            dxf.AddEntity(dim2);*/
            fun.dimrot1(dxf, v11, new Vector2(0, 0), 90, offset * 2 + pdx / 2, 1, height, arrowsize, exline, "PDY"/* + Math.Round(pdy / scale, 2)*/, "", "");

            //尺寸3
            /*LinearDimension dim3 = new LinearDimension(new Line(v11, v4), -offset * 3 - pdx / 2, 270, dimensionStyle);
            dim3.UserText = "INSL = " + Math.Round(insl / scale, 2);
            dxf.AddEntity(dim3);*/
            fun.dimrot1(dxf, v11, v4, 90, offset * 3 + pdx / 2, 1, height, arrowsize, exline, "INSL" /*+ Math.Round(insl / scale, 2)*/, "", "");

            //尺寸4
            /*LinearDimension dim4 = new LinearDimension(new Line(v4, v10), offset + (v4.Y - v10.Y) / 2, 0, dimensionStyle);
            dim4.UserText = "W1 = " + Math.Round(w1 / scale, 2);
            dxf.AddEntity(dim4);*/
            fun.dimrot1(dxf, v4, v10, 0, offset + (v4.Y - v10.Y) / 2, 1, height, arrowsize, exline, "W1" /*+ Math.Round(w1 / scale, 2)*/, "", "");

            //尺寸5
            RadialDimension dimrad1 = new RadialDimension(arc1, -135 - ral / 2 * 180 / pi, 1, dimensionStyle);
            dimrad1.UserText = "REL" /*+ Math.Round(rel / scale, 2)*/;
            dxf.AddEntity(dimrad1);

            //尺寸6
            RadialDimension dimrad2 = new RadialDimension(arc2, -45 - rar / 2 * 180 / pi, offset, dimensionStyle);
            dimrad2.UserText = "RER" /*+ Math.Round(rer / scale, 2)*/;
            dxf.AddEntity(dimrad2);

            //尺寸7
            /*Angular2LineDimension angdim1 = new Angular2LineDimension(Line18, Line17, offset * 2, dimensionStyle);
            angdim1.UserText = "RAL" /*+ Math.Round(ral / (pi / 180), 2) + "°"*/;
            //dxf.AddEntity(angdim1);

            //尺寸8
            /*Angular2LineDimension angdim2 = new Angular2LineDimension(new Line(v10, v9), Line19, offset * 2, dimensionStyle);
            angdim2.UserText = "RAR" /*+ Math.Round(rar / (pi / 180), 2) + "°"*/;
            //dxf.AddEntity(angdim2);

            //尺寸9
            /*LinearDimension dim9 = new LinearDimension(new Line(v1, v11), -2.5 * offset - (v1.Y - v11.Y) / 2 * 3, 0, dimensionStyle);
            dim9.UserText = "PDX = " + Math.Round(pdx / scale, 2);
            dxf.AddEntity(dim9);*/
            fun.dimrot1(dxf, v1, v11, 180, offset * 5, 1, height, arrowsize, exline, "PDX" /*+ Math.Round(pdx / scale, 2)*/, "", "");

            //尺寸10
            /*LinearDimension dim10 = new LinearDimension(new Line(v11, v10), -2.5 * offset - 2 * dimensionStyle.TextHeight * 3, 0, dimensionStyle);
            dim10.UserText = "CW = " + Math.Round(cw / scale, 2);
            dxf.AddEntity(dim10);*/
            fun.dimrot1(dxf, v11, v10, 180, offset * 3.5, 1, height, arrowsize, exline, "CW" /*+ Math.Round(cw / scale, 2)*/, "", "");

            //尺寸11
            var dim11 = new DiametricDimension(circle1, 45, br + offset, dimensionStyle);
            dim11.UserText = "D1" /*+ Math.Round(d1 / scale, 2)*/;
            dxf.AddEntity(dim11);

            //尺寸12
            LinearDimension dim12 = new LinearDimension(new Line(v27, v28), -offset - (v27.Y - v28.Y) / 2, 0, dimensionStyle);
            dim12.UserText = "S" /*+ Math.Round(s / scale, 2)*/;
            dxf.AddEntity(dim12);

            //尺寸13
            Angular2LineDimension dim13 = new Angular2LineDimension(new Line(v28, v34), new Line(v28, v35), offset + s, dimensionStyle);
            dim13.UserText = "AN" /*+ Math.Round(an / (pi / 180), 2) + "°"*/;
            dxf.AddEntity(dim13);

            //尺寸14
            Angular2LineDimension dim14 = new Angular2LineDimension(Line25, Line26, offset, dimensionStyle);
            dim14.UserText = "GAN" /*+ Math.Round(gan / (pi / 180), 2) + "°"*/;
            dxf.AddEntity(dim14);

            //坐标系
            /*fun.Axis(dxf, new Vector2(0, 0), 180, 270, "MCS,PCS", "", "", 0.3, height, 1);
            fun.Axis(dxf, new Vector2(v1.X - w1, v1.Y - pdpt * 3), 180, 270, "Z", "X", "Y", 0.3, height, 1);//XYZ
            fun.Axis(dxf, v25, 0, 270, "MSC,PCS", "", "", 0.3, height, 1);*/

            //图框
            if (isframe == 1)
            {
                Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2((v10.X + v26.X) / 2 - 148.5, v26.Y + 105), data);
                fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
            }
           
            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);
            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
